<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BeeSoft</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
        }

        h1 {
            color: white;
            text-align: center;
            margin-bottom: 30px;
            font-size: 2.5rem;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }

        button {
            padding: 12px 24px;
            font-size: 16px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            background: white;
            color: #667eea;
            font-weight: 600;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.2);
        }

        button:active {
            transform: translateY(0);
        }

        .data-section {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(600px, 1fr));
            gap: 30px;
            margin-bottom: 30px;
        }

        .data-card {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .data-card h2 {
            color: #667eea;
            margin-bottom: 20px;
            font-size: 1.8rem;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }

        .data-item {
            background: #f8f9fa;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            transition: all 0.3s ease;
        }

        .data-item:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }

        .data-item strong {
            color: #667eea;
            display: inline-block;
            min-width: 120px;
        }

        .json-data {
            background: #2d3748;
            color: #68d391;
            padding: 10px;
            border-radius: 5px;
            margin-top: 8px;
            font-family: 'Courier New', monospace;
            font-size: 13px;
            overflow-x: auto;
            white-space: pre-wrap;
            word-wrap: break-word;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: white;
            font-size: 1.2rem;
        }

        .error {
            background: #fed7d7;
            color: #c53030;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #c53030;
        }

        .success {
            background: #c6f6d5;
            color: #22543d;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #22543d;
        }

        .badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 600;
            margin-left: 10px;
        }

        .badge-primary {
            background: #667eea;
            color: white;
        }

        .badge-secondary {
            background: #cbd5e0;
            color: #2d3748;
        }

        @media (max-width: 768px) {
            .data-section {
                grid-template-columns: 1fr;
            }

            h1 {
                font-size: 1.8rem;
            }

            button {
                padding: 10px 20px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>BeeSoft Base</h1>
        
        <div class="controls">
            <button onclick="loadLanguages()">📚 加载语言数据</button>
            <button onclick="loadCountries()">🌏 加载国家数据</button>
            <button onclick="loadAll()">🔄 加载全部数据</button>
            <button onclick="clearData()">🗑️ 清空显示</button>
        </div>

        <div id="message"></div>

        <div class="data-section">
            <div class="data-card">
                <h2>📚 语言</h2>
                <div id="languagesContainer" class="loading">点击"加载语言数据"按钮查看数据</div>
            </div>

            <div class="data-card">
                <h2>🌏 国家</h2>
                <div id="countriesContainer" class="loading">点击"加载国家数据"按钮查看数据</div>
            </div>
        </div>
    </div>

    <script>
        function showMessage(message, type = 'success') {
            const messageDiv = document.getElementById('message');
            messageDiv.className = type;
            messageDiv.textContent = message;
            setTimeout(() => {
                messageDiv.textContent = '';
                messageDiv.className = '';
            }, 3000);
        }

        function parseJsonTitle(title) {
            try {
                const data = JSON.parse(title);
                return `<div class="json-data">${JSON.stringify(data, null, 2)}</div>`;
            } catch (e) {
                return `<div class="json-data">${title}</div>`;
            }
        }

        async function loadLanguages() {
            const container = document.getElementById('languagesContainer');
            container.innerHTML = '<div class="loading">⏳ 加载中...</div>';

            try {
                const response = await fetch('/api/languages');
                if (!response.ok) throw new Error('加载失败');
                
                const languages = await response.json();
                
                if (languages.length === 0) {
                    container.innerHTML = '<div class="loading">暂无数据</div>';
                    return;
                }

                let html = '';
                let parentLanguages = languages.filter(l => !l.parentId);
                let childLanguages = languages.filter(l => l.parentId);

                // 显示主语言
                parentLanguages.forEach(lang => {
                    const children = childLanguages.filter(c => c.parentId === lang.id);
                    html += `
                        <div class="data-item">
                            <div>
                                <strong>代码:</strong> ${lang.code}
                                <span class="badge badge-primary">主语言</span>
                            </div>
                            <div><strong>ID:</strong> ${lang.id}</div>
                            <div><strong>创建时间:</strong> ${new Date(lang.createdAt).toLocaleString('zh-CN')}</div>
                            <div><strong>国际化标题:</strong></div>
                            ${parseJsonTitle(lang.title)}
                            ${children.length > 0 ? `<div style="margin-top: 10px;"><strong>变体 (${children.length}):</strong> ${children.map(c => c.code).join(', ')}</div>` : ''}
                        </div>
                    `;
                });

                // 显示语言变体
                if (childLanguages.length > 0) {
                    html += '<div style="margin: 20px 0; padding: 10px; background: #e9ecef; border-radius: 8px;"><strong>语言变体:</strong></div>';
                    childLanguages.forEach(lang => {
                        const parent = parentLanguages.find(p => p.id === lang.parentId);
                        html += `
                            <div class="data-item">
                                <div>
                                    <strong>代码:</strong> ${lang.code}
                                    <span class="badge badge-secondary">变体</span>
                                </div>
                                <div><strong>ID:</strong> ${lang.id}</div>
                                <div><strong>父语言:</strong> ${parent ? parent.code : 'N/A'}</div>
                                <div><strong>创建时间:</strong> ${new Date(lang.createdAt).toLocaleString('zh-CN')}</div>
                                <div><strong>国际化标题:</strong></div>
                                ${parseJsonTitle(lang.title)}
                            </div>
                        `;
                    });
                }

                container.innerHTML = html;
                showMessage(`✅ 成功加载 ${languages.length} 条语言数据`);
            } catch (error) {
                container.innerHTML = `<div class="error">❌ 加载失败: ${error.message}</div>`;
                showMessage('❌ 加载语言数据失败', 'error');
            }
        }

        async function loadCountries() {
            const container = document.getElementById('countriesContainer');
            container.innerHTML = '<div class="loading">⏳ 加载中...</div>';

            try {
                const response = await fetch('/api/countries');
                if (!response.ok) throw new Error('加载失败');
                
                const countries = await response.json();
                
                if (countries.length === 0) {
                    container.innerHTML = '<div class="loading">暂无数据</div>';
                    return;
                }

                let html = '';
                countries.forEach(country => {
                    html += `
                        <div class="data-item">
                            <div>
                                <strong>代码:</strong> ${country.code}
                                <span class="badge badge-primary">国家</span>
                            </div>
                            <div><strong>ID:</strong> ${country.id}</div>
                            <div><strong>创建时间:</strong> ${new Date(country.createdAt).toLocaleString('zh-CN')}</div>
                            <div><strong>国际化标题:</strong></div>
                            ${parseJsonTitle(country.title)}
                        </div>
                    `;
                });

                container.innerHTML = html;
                showMessage(`✅ 成功加载 ${countries.length} 条国家数据`);
            } catch (error) {
                container.innerHTML = `<div class="error">❌ 加载失败: ${error.message}</div>`;
                showMessage('❌ 加载国家数据失败', 'error');
            }
        }

        async function loadAll() {
            await Promise.all([loadLanguages(), loadCountries()]);
        }

        function clearData() {
            document.getElementById('languagesContainer').innerHTML = '<div class="loading">点击"加载语言数据"按钮查看数据</div>';
            document.getElementById('countriesContainer').innerHTML = '<div class="loading">点击"加载国家数据"按钮查看数据</div>';
            showMessage('🗑️ 已清空显示');
        }

        // 页面加载时自动加载数据
        window.addEventListener('load', () => {
            loadAll();
        });
    </script>
</body>
</html>
